////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2001-2005.
// -------------------------------------------------------------------------
//  File name:   SolidBrushPanel.h
//  Version:     v1.00
//  Created:     11/1/2005 by Timur.
//  Compilers:   Visual Studio.NET 2003
//  Description: CSolidBrushPanel implementation file.
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "SolidBrushPanel.h"

#include "Brush\SolidBrushObject.h"
#include ".\solidbrushpanel.h"
#include "SegmentedWorld/SegmentedWorldManager.h"

// CSolidBrushPanel dialog

IMPLEMENT_DYNAMIC(CSolidBrushPanel, CXTResizeDialog)
CSolidBrushPanel::CSolidBrushPanel(CWnd* pParent /*=NULL*/)
	: CXTResizeDialog(CSolidBrushPanel::IDD, pParent)
{
	Create( IDD,pParent );
}

CSolidBrushPanel::~CSolidBrushPanel()
{
}

void CSolidBrushPanel::DoDataExchange(CDataExchange* pDX)
{
	CXTResizeDialog::DoDataExchange(pDX);
	DDX_Control( pDX,IDC_GEOMETRY_EDIT,m_geomEdit );
	DDX_Control( pDX,IDC_SAVE_TO_CGF,m_btn[0] );
	DDX_Control( pDX,IDC_RESET_TRANSFORM,m_btn[1] );
	DDX_Control( pDX,IDC_SNAP_TO_GRID,m_btn[2] );
	DDX_Control( pDX,IDC_UV_MAPPING,m_btn[3] );
	DDX_Control( pDX,IDC_SOLID_MERGE,m_btn[4] );
	DDX_Control( pDX,IDC_CLIP_TOOL,m_btn[5] );
	DDX_Control( pDX,IDC_SOLIDCSG_UNION,m_btn[6] );
	DDX_Control( pDX,IDC_SOLIDCSG_INTERSECTION,m_btn[7] );
	DDX_Control( pDX,IDC_SOLIDCSG_DIFFERENCE,m_btn[8] );
}

void CSolidBrushPanel::SetEnable( bool bEnable)
{
	GetDlgItem(IDC_GEOMETRY_EDIT)->EnableWindow(bEnable);
	GetDlgItem(IDC_SAVE_TO_CGF)->EnableWindow(bEnable);
	GetDlgItem(IDC_RESET_TRANSFORM)->EnableWindow(bEnable);
	GetDlgItem(IDC_SNAP_TO_GRID)->EnableWindow(bEnable);
	GetDlgItem(IDC_UV_MAPPING)->EnableWindow(bEnable);
	GetDlgItem(IDC_CLIP_TOOL)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLID_MERGE)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLIDCSG_UNION)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLIDCSG_INTERSECTION)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLIDCSG_DIFFERENCE)->EnableWindow(bEnable);
}

void CSolidBrushPanel::SetEnableMultiSel(bool bEnable, bool bAllOfSameType)
{
	GetDlgItem(IDC_GEOMETRY_EDIT)->EnableWindow(bEnable);
	GetDlgItem(IDC_RESET_TRANSFORM)->EnableWindow(bEnable);
	GetDlgItem(IDC_SNAP_TO_GRID)->EnableWindow(bEnable);
	GetDlgItem(IDC_UV_MAPPING)->EnableWindow(bEnable);
	GetDlgItem(IDC_CLIP_TOOL)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLID_MERGE)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLIDCSG_UNION)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLIDCSG_INTERSECTION)->EnableWindow(bEnable);
	GetDlgItem(IDC_SOLIDCSG_DIFFERENCE)->EnableWindow(bEnable);

	GetDlgItem(IDC_SAVE_TO_CGF)->EnableWindow(FALSE);
}

BEGIN_MESSAGE_MAP(CSolidBrushPanel, CXTResizeDialog)
	ON_BN_CLICKED(IDC_SAVE_TO_CGF, OnSaveToCgf)
	ON_BN_CLICKED(IDC_RESET_TRANSFORM, OnBnClickedResetTransform)
	ON_BN_CLICKED(IDC_SNAP_TO_GRID, OnBnClickedSnapToGrid)
	ON_BN_CLICKED(IDC_UV_MAPPING, OnBnClickedUvMapping)
	ON_BN_CLICKED(IDC_CLIP_TOOL, OnBnClickedClipTool)
	ON_BN_CLICKED(IDC_SOLID_MERGE, OnBnClickedMerge)
	ON_BN_CLICKED(IDC_SOLIDCSG_UNION, OnBnClickedSolidCSGUnion)
	ON_BN_CLICKED(IDC_SOLIDCSG_INTERSECTION, OnBnClickedSolidCSGIntersection)
	ON_BN_CLICKED(IDC_SOLIDCSG_DIFFERENCE, OnBnClickedSolidCSGDifference)
END_MESSAGE_MAP()


// CSolidBrushPanel message handlers
BOOL CSolidBrushPanel::OnInitDialog()
{
	BOOL res = CXTResizeDialog::OnInitDialog();

	m_geomEdit.SetToolName( "EditTool.SubObjMode" );

	CSelectionGroup* pSel = GetIEditor()->GetSelection();
	if(pSel && pSel->GetCount()>1)
	{
		GetDlgItem(IDC_SAVE_TO_CGF)->EnableWindow(FALSE);
	}

	SetResize(IDC_STATIC_MISC, SZ_HORRESIZE(1.0f));
	SetResize(IDC_STATIC_CSG, SZ_HORRESIZE(1.0f));
	SetResize(IDC_GEOMETRY_EDIT, SZ_HORRESIZE(1.0f));

	SetResize(IDC_RESET_TRANSFORM, SZ_HORRESIZE(0.5f));
	SetResize(IDC_SAVE_TO_CGF, CXTResizeRect(0.5f, 0.0f, 1.0f, 0.0f));

	SetResize(IDC_UV_MAPPING, SZ_HORRESIZE(0.5f));
	SetResize(IDC_SNAP_TO_GRID, CXTResizeRect(0.5f, 0.0f, 1.0f, 0.0f));

	SetResize(IDC_SOLID_MERGE, SZ_HORRESIZE(0.5f));
	SetResize(IDC_CLIP_TOOL, CXTResizeRect(0.5f, 0.0f, 1.0f, 0.0f));

	SetResize(IDC_SOLIDCSG_UNION, SZ_HORRESIZE(0.5f));
	SetResize(IDC_SOLIDCSG_INTERSECTION, CXTResizeRect(0.5f, 0.0f, 1.0f, 0.0f));

	SetResize(IDC_SOLIDCSG_DIFFERENCE, SZ_HORRESIZE(0.5f));
	
	return res;
}

//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::SetBrush( CSolidBrushObject *pObject )
{
	m_pObject = pObject;
}

//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::OnSaveToCgf()
{
	if (!m_pObject)
		return;

	CString filename;	
	if (CFileUtil::SelectSaveFile( "CGF Files|*.cgf","*.cgf",Path::GetGameFolder()+CString("\\objects"),filename ))
	{
		m_pObject->SaveToCgf( filename );
		SW_ON_OBJ_MOD(m_pObject);
	}
}

//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::OnBnClickedResetTransform()
{
	GetIEditor()->ExecuteCommand( "Brush.ResetTransform" );
}
//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::OnBnClickedSnapToGrid()
{
	GetIEditor()->ExecuteCommand( "Brush.SnapPointsToGrid" );
}

//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::OnCancel()
{
	GetIEditor()->ClearSelection();
}
//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::OnBnClickedUvMapping()
{
	GetIEditor()->SetEditTool("EditTool.TextureBrush",true);
}
//////////////////////////////////////////////////////////////////////////
void CSolidBrushPanel::OnBnClickedClipTool()
{
	GetIEditor()->SetEditTool("EditTool.ClipBrush",true);
}
//////////////////////////////////////////////////////////////////////////

void CSolidBrushPanel::OnBnClickedMerge()
{
	GetIEditor()->ExecuteCommand( "Brush.MergeBrush" );	
}


void CSolidBrushPanel::OnBnClickedSolidCSGUnion()
{
	GetIEditor()->ExecuteCommand( "Brush.CSGUnionBrush" );	
}

void CSolidBrushPanel::OnBnClickedSolidCSGIntersection()
{
	GetIEditor()->ExecuteCommand( "Brush.CSGIntersectionBrush" );
}

void CSolidBrushPanel::OnBnClickedSolidCSGDifference()
{
	GetIEditor()->ExecuteCommand( "Brush.CSGDifferenceBrush" );
}
